package com.opo5.logisticsmanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.domain.Ledger;
import com.opo5.logisticsmanagement.domain.New;
import com.opo5.logisticsmanagement.service.LedgerService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("ledger")
public class LedgerController {
    @Autowired
    private LedgerService ledgerService;
//    @GetMapping("/{currentPage}/{pageSize}/{startTime}/{finishTime}")
//    R getAll(@PathVariable int currentPage, @PathVariable int pageSize, @PathVariable String startTime, @PathVariable String finishTime, Ledger ledger){
////        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
////        startTime=sdf.parse(startTime);
//        IPage<Ledger> page = ledgerService.selectPage(currentPage, pageSize, startTime,finishTime,ledger);
//        return R.success(page);
//    }

    @PostMapping()
    R getAll(@RequestBody LedgerCondition lc){
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        startTime=sdf.parse(startTime);
        if(lc.getStartTime().isEmpty()){
            lc.setStartTime(null);
        }
        if(lc.getEndTime().isEmpty()){
            lc.setEndTime(null);
        }

        Ledger ledger=new Ledger();

        System.out.println(lc.getLedger());

        if(lc.getLedger().getOperatetype().isEmpty()){
//            ledger.setOperatetype(null);
//            lc.setLedger(ledger);
            lc.getLedger().setOperatetype(null);
        }
        if(lc.getLedger().getPlantype().isEmpty()){
            lc.getLedger().setPlantype(null);
        }
        IPage<Ledger> page = ledgerService.selectPage1(lc.getCurrentPage(),lc.getPageSize(), lc.getStartTime(), lc.getEndTime(),lc.getLedger());
        return R.success(page);
    }
    @GetMapping("/{currentPage}/{pageSize}")
    R getBilling(@PathVariable int currentPage,@PathVariable int pageSize,Ledger ledger){

        IPage<Ledger> page = ledgerService.selectBilling(currentPage,pageSize,ledger);
        return R.success(page);
    }

    /**
     *
     * @param ledger
     * @return
     */
    @PutMapping
    public R putList(@RequestBody Ledger ledger){
        if (ledger.getBillingstate()=="已结算"){
            ledger.setBillingstate("未结算");
        }else{
            ledger.setBillingstate("已结算");
        }
        ledgerService.updateById(ledger);
        return R.success("修改成功");
    }
}
